<?php

	/**
    * @framework : My Framework
    *
    * @author: fatangel26@gmail.com - Nguyễn Bảo Trung.
    *
    */

	class model_cart extends  my_model
	{

		public $cart_contents = array();

		public function __construct()
		{
			parent::__construct();

			$this->_helper = new helper_session();

			$this->cart_contents = $this->_helper->get_userdata('jewel_cart_contents');
		}

		public function create($data = array())
        {
            if(!empty($data))
            {
                $this->insert('tbl_customer', $data);

                if($this->affected_row() > 0)
                {
                    $customer_id = $this->insert_id();

                    if($this->create_order($customer_id, $_POST['customer_comment']))
                    {
                        return TRUE;
                    }
                    else
                    {
                    	$this->where(array('customer_id' => $customer_id));
                        $this->delete('tbl_customer');
                        return FALSE;
                    }

                }
                else
                {
                    return FALSE;
                }

            }//End else
        }

        public function create_order($customer_id, $customer_cm)
        {
            $strInsert = array(
                'customer_id'   => $customer_id,
                'total'         => $this->cart_contents['cart_total'],
                'customer_cm'   => $customer_cm,
                'order_date'	=> 'NOW()'
            );

            $this->insert('tbl_order', $strInsert);

            if($this->affected_row() > 0)
            {
                $order_id = $this->insert_id();

                if($this->create_order_item($order_id))
                {
                    return TRUE;
                }
                else
                {
                	$this->where(array('order_id' => $order_id));
                    $this->delete('tbl_order');
                    return FALSE;
                }
            }
            else
            {
                return FALSE;
            }

        }//End function

        public function create_order_item($order_id)
        {
            if($this->cart_contents)
            {

                foreach($this->cart_contents as $item):
                    $strInsert = array(
                        'order_id'  => $order_id,
                        'product_id'=> $item['id'],
                        'quantity'  => $item['quantity'],
                        'price'     => $item['price']
                    );

                    $query = $this->insert('tbl_order_item', $strInsert);

                    $this->update_quantity($item['id'], $item['qty']);

                endforeach;

                return TRUE;
            }
            else
            {
                return FALSE;
            }
        }

        public function update_quantity($product_id, $quantity)
        {
            $strSQL = "UPDATE tbl_product SET product_quantity = product_quantity - ".$quantity." WHERE product_id = ".$product_id;

            $this->query($strSQL);
        }
	}

	/* End of file model_cart.php */
	/* Location: .//D/root/my_framework/models/model_cart.php */
